//
//  WBTopView.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/13.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit
import SnapKit

class WBTopView: UIView {
    
    
    var topViewBottomConstraint : Constraint?
    
    var statusViewModel: WBStatusViewModel?{
        
        didSet{
            //给头像赋值
            headImageView.wb_setImageWithString(statusViewModel?.statusModel?.user?.profile_image_url, placeholderImageName: "avatar_default")
            //给name赋值
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            
            timeLabel.text = statusViewModel?.sinaCreatTimeStr
            
            sourceLabel.text = statusViewModel?.sourceString
            //给正文内容赋值
            contentLabel.attributedText = statusViewModel?.orginalAttr
            //给会员图片赋值
            membershipImageView.image = statusViewModel?.membershipImage
            
            //给认证图片赋值
            avatarImageView.image = statusViewModel?.avatarImage
            
            
            //卸载约束
            topViewBottomConstraint?.uninstall()
            //判断有没有配图
            if statusViewModel?.statusModel?.pic_urls?.count > 0{
                //有配图
                //赋值
                pictureView.pictureInfo = statusViewModel?.statusModel?.pic_urls
                //更新约束
                self.snp_makeConstraints(closure: { (make) in
                    topViewBottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(10).constraint
                })
                //显示控件
                pictureView.hidden = false
            }else{
                
                //没配图
                //更新约束
                self.snp_makeConstraints(closure: { (make) in
                    topViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                //隐藏控件
                pictureView.hidden = true
                
            }
            
        }
        
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        headImageView.snp_makeConstraints { (make) in
            //左上距离10 大小35
            make.top.leading.equalTo(self).offset(10)
            make.size.equalTo(CGSizeMake(35, 35))
        }
        nameLabel.snp_makeConstraints { (make) in
            //左边距离头像10 顶部和图片相等
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
            make.top.equalTo(headImageView)
        }
        membershipImageView.snp_makeConstraints { (make) in
            //左边距离namelabel为10 中心Y和namelabel相等
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        timeLabel.snp_makeConstraints { (make) in
            //左边和namelabel一样 底部和headimage一样
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        sourceLabel.snp_makeConstraints { (make) in
            //左边和timelabel 右边距离10 底部和timelabel相等
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        avatarImageView.snp_makeConstraints { (make) in
            //x -> 右边 y -> 下边
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) in
            //顶部=headImage底部+ 10 左边距离10
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.leading.equalTo(self).offset(10)
        }
        
        pictureView.snp_makeConstraints { (make) in
            //宽高是100
            //make.size.equalTo(CGSizeMake(100, 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo((ScreenW - 250) * 0.5)
        }
        
        self.snp_makeConstraints { (make) in
            //底部和contentLabel底部距离为10
            // make.bottom.equalTo(contentLabel).offset(10)
            topViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        
    }
    
    //懒加载头像图片
    private lazy var headImageView : UIImageView = UIImageView(imgName: "avatar_default")
    //懒加载名字label
    private lazy var nameLabel : UILabel = {
        
        //创建一个label 文字大小15 字体颜色
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        label.text = "名字"
        
        return label
    }()
    //懒加载用户等级
    private lazy var membershipImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    //懒加载时间label
    private lazy var timeLabel: UILabel = UILabel(textColor: AppColor, textSize: 10)
    //懒加载来源label
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor() , textSize: 10)
    //懒加载认证图片
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    //懒加载正文内容
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textSize: 15, maxWidth: ScreenW - 20)
    //懒加载配图collectionview
    private lazy var pictureView: WBStatusPictureView = WBStatusPictureView()
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
